#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

int totalFruit(vector<int>& fruits)
{
    int kinds = 0;
    unordered_map<int, int> hash;
    int left = 0, right = 0;
    for (right = 0; right < fruits.size(); right++)
    {
        hash[fruits[right]]++;
        while (hash.size() > 2)
        {
            hash[fruits[left]]--;
            if (hash[fruits[left]] == 0)
            {
                hash.erase(fruits[left]);
            }
            left++;
        }

        kinds = max(kinds, right
            - left + 1);

    }
    return kinds;
}

int main()
{
    vector<int> f = { 1,2,1 };
    cout << totalFruit(f) << endl;
    return 0;
}